package com.briup.estore.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.ShopCar;
import com.briup.estore.bean.ShopCarItem;
import com.briup.estore.mapper.IBookMapper;
import com.briup.estore.mapper.IShopCarMapper;
import com.briup.estore.service.IShopCarService;

@Service
public class ShopCarServiceImpl implements IShopCarService {

	@Autowired
	private IBookMapper bookMapper;

	@Autowired
	private IShopCarMapper shopCarMapper;

	@Override
	@Transactional
	public void addOrderLineToShopCar(int num, BigInteger book_id, BigInteger customer_id) {
		// 1 查书籍信息
		Book book = bookMapper.findBookById(book_id);
		// 2 计算订单信息
		double cost = book.getPrice() * num;
		// 3 查询购物车信息
		ShopCar shopCar = shopCarMapper.findShopCarByCUstomerId(customer_id);
		// 4 如果没有购物车就创建一个,更新购物车信息
		if (shopCar == null) {
			shopCar = new ShopCar();
			shopCar.setCustomer_id(customer_id);
			shopCar.setTotal(0);
			shopCarMapper.newShopCar(shopCar);
		}
		// 5 添加订单信息
		ShopCarItem shopCarItem = new ShopCarItem();
		shopCarItem.setNum(new BigInteger(num + ""));
		shopCarItem.setCost(cost);
		shopCarItem.setBook_id(book_id);
		shopCarItem.setShopcar_id(shopCar.getId());

		// 6 保存购物车项，更新购物车
		shopCar.setTotal(shopCar.getTotal() + (shopCarItem.getCost()));
		shopCarMapper.insertShopCarItem(shopCarItem);
		shopCarMapper.updateShopCar(shopCar);
	}

	@Override
	public ShopCar findShopCarWithShipCarItemByCustomerId(BigInteger customer_id) {
		ShopCar shopCar = shopCarMapper.findShopCarWithShopCarItemWIthBookByCustomerId(customer_id);
//		System.out.println("service-shopCar>>>" + shopCar);
		return shopCar;
	}

	@Override
	public ShopCar findShopCarByCustomerId(BigInteger customer_id) {
		return shopCarMapper.findShopCarByCUstomerId(customer_id);
	}

	@Override
	public ShopCarItem findShopCarItemById(BigInteger id) {
		return shopCarMapper.findShopCarItemById(id);
	}

	@Override
	@Transactional
	public int deleteShopCarItem(BigInteger customer_id, BigInteger item_id) {
		// 查询购物车
		ShopCar shopCar = shopCarMapper.findShopCarByCUstomerId(customer_id);
		// 查询购物车项
		ShopCarItem shopCarItem = shopCarMapper.findShopCarItemById(item_id);
		if (shopCar == null || shopCarItem == null) {
			return -1;
		}
		// 删除购物车子项
		shopCarMapper.deleteShopCarItem(item_id);
		// 更新购物车
		shopCar.setTotal(shopCar.getTotal() - shopCarItem.getCost());
		return shopCarMapper.updateShopCar(shopCar);
	}

	@Override
	public List<ShopCarItem> findShopCarItemByIds(List<BigInteger> select) {

		if (select == null || select.size() == 0) {
			return new ArrayList<ShopCarItem>();
		}
		return shopCarMapper.findShopCarItemByIds(select);
	}

}
